import os
import struct
#sorted sid list
sss = []
with open('sid.txt','r') as f:
    lines=f.readlines()
    i = 0
    sids = []
    for line in lines:
        i = i + 1
        if len(line) > 1:
            x = int(line, 10)
            sids.append(x)
            print("line: " + str(i) + " : " + str(x))
    sss = sorted(list(set(sids)))
    print(sss)
    print("total len: " + str(len(sss)))

#file list
fs = []
for dirpath, dirnames, filenames in os.walk('../snortrules/'):
    for filename in filenames:
        fs.append(os.path.join(dirpath, filename))

#list file in file-list
#      then loop sorted sid list
#         to find out the exact sid
survives = sss
xout = open('shot.rules','w')
for f in fs:
    if f.find("deleted") ==  -1:
        print("file: " + f)
    with open(f,'r') as fi:
        lines=fi.readlines()
        shots = []
        for line in lines:
            i = 0
            for s in survives:
                #if s == 14620:
                #    print("s is 14620")
                match = "sid:"+str(s)+";"
                if line.find(match)!= -1:
                   del survives[i]
                   #write this line into shot.rules
                   xout.write(line)
                   break
                i = i + 1

xout.close()
print(survives)
print("total survives: " + str(len(survives)))
